<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\model;

use app\common\model\Store as StoreModel;

/**
 * 商家记录表模型
 * Class Store
 * @package app\store\model
 */
class Store extends StoreModel
{
    /**
     * 更新记录
     * @param array $data
     * @return bool
     */
    public function edit(array $data): bool
    {
        // 是否删除图片
        !isset($data['logo_image_id']) && $data['logo_image_id'] = 0;
        return $this->save($data) !== false;
    }

    /**
     * 更新记录
     * @param array $data
     * @return bool
     */
    public function editbyMobile(array $data,$storeId): bool
    {
        // 是否删除图片
        !isset($data['logo_image_id']) && $data['logo_image_id'] = 0;
        $storeData['store_name'] = $data['store_name'];
        $storeData['describe'] = $data['describe'];
        $storeData['phone'] = $data['phone'];
        $storeData['logo_image_id'] = $data['logo_image_id'];
        $storeData['business_time'] = $data['business_time'];
        $storeData['express_price'] = $data['express_price'];
        $storeData['is_online'] = $data['is_online'];
        //更新折扣
        $discount = [];
        $discount['discount'] = (float)$data['discount'];
        if(UserGrade::where('store_id', $storeId)->find()){
            UserGrade::where('store_id', $storeId)->update(['equity' => json_encode($discount),'update_time'=> time()]);
        }else{
            $upgrade = [];
            $upgrade['expend_money'] = 10000000;
            $gradeData['store_id'] = $storeId;
            $gradeData['name'] = '黄金会员';
            $gradeData['weight'] = 1;
            $gradeData['create_time'] = time();
            $gradeData['update_time'] =  time();
            $gradeData['discount'] = json_encode($discount);
            $gradeData['upgrade'] = json_encode($upgrade);
            UserGrade::create($gradeData);
        }
        //更新外卖时间
        if($data['value_z1'] && $data['value_z2']){
            $update['breakfast'] = $data['value_z1'].",". $data['value_z2'];
        }else{
            $update['breakfast'] = "";
        }
        if($data['value_m1'] && $data['value_m2']){
            $update['lunch'] = $data['value_m1'].",". $data['value_m2'];
        }else{
            $update['lunch'] = "";
        }
        if($data['value_x1'] && $data['value_x2']){
            $update['dinner'] = $data['value_x1'].",". $data['value_x2'];
        }else{
            $update['dinner'] = "";
        }

        if(StoreDelivery::where('store_id', $storeId)->find()){
            StoreDelivery::where('store_id', $storeId)->update($update);
        }else{
            $update['store_id'] = $storeId;
            StoreDelivery::create($update);
        }

        return $this->save($storeData) !== false;
    }
}
